/*
 * Check.java
 *
 * Created on August 29, 2008, 9:24 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Project.bussiness;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author haolt_c0708l
 */
public class Check {

    public static boolean checkName(String n)
    {
        if (n==null) return false;
        else
        {
            String strPattern = "[^a-z ]";
            Pattern p; Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern,flag);
            m=p.matcher(n);
            return !m.find();
        }
    }
    public static boolean checkEmail(String n)
    {
        if (n==null) return false;
        else
        {
            String strPattern = "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b";
            Pattern p; Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern,flag);
            m=p.matcher(n);
            return m.find();
        }
    }
    public static boolean checkPhone(String n)
    {
        if (n==null) return false;
        else
        {
            String strPattern = "[^0-9 .]";
            Pattern p;Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern,flag);
            m = p.matcher(n);            
            return !m.find();
        }
    }
    public static boolean checkDate(String n)
    {
        if (n==null) return false;
        else
        {
            boolean flag=true;
            String dd,mm,yyyy;
            int d,m,y;
            mm=n.substring(0,2);
            dd=n.substring(3,5);
            yyyy=n.substring(6,10);
            try{
            m=Integer.parseInt(mm);
            d=Integer.parseInt(dd);
            y=Integer.parseInt(yyyy);
            if ((d<1)&&(d>31)) flag=false;
            if ((m<1)&&(m>12)) flag=false;
            if (y<1) flag=false;
            }catch(Exception e){flag=false;}
            return flag;
        }
    }
    public static boolean checkDateInputOutput(String in,String out)
    {
            boolean flag=true;        
            String dd,mm,yyyy;
            int d1=0,m1=0,y1=0;
            mm=in.substring(0,2);
            dd=in.substring(3,5);
            yyyy=in.substring(6,10);
            try{
            m1=Integer.parseInt(mm);
            d1=Integer.parseInt(dd);
            y1=Integer.parseInt(yyyy);
            }catch(Exception e){flag=false;}
            int d2=0,m2=0,y2=0;
            mm=out.substring(0,2);
            dd=out.substring(3,5);
            yyyy=out.substring(6,10);
            try{
            m2=Integer.parseInt(mm);
            d2=Integer.parseInt(dd);
            y2=Integer.parseInt(yyyy);
            }catch(Exception e){flag=false;}     
            if (y1>y2) flag= false;
            else 
                if (y1==y1)
                {
                    if(m1>m2) flag= false;
                    else if (m1==m2)
                    {
                        if (d1>d2) flag=false;
                    }
                }
        return flag;
    }
}
